<?php
/*
-- OVERVIEW
Provides graphical add form for new entries
Can this be provided with a type of form (AutoForm) class?
&-> Filters 

*/


class CF_AddBox{

	var $fieldspec;
	var $table_name;
	var $actionpage;
	var $thefilters;
	var $filterprefix;
	var $actval_prefix;
	var $top_dir;
	
	private $lookup_lists;
	
	public static $ctl_mapping = array('ctl_text'=>'CtlText','ctl_select'=>'CtlSelect','ctl_textarea'=>'CtlTextArea','ctl_jqdate'=>'CtlJQDate','ctl_checkbox'=>'CtlCheckbox');
		
	
	public static $none_entry = array(
		0=>array('keyvalue' => '', 'showvalue' => '')
		
	);
	
	public static $yesno_entry = array(
		0=>array('keyvalue' => '', 'showvalue' => ''),
		1=>array('keyvalue' => 0, 'showvalue' => 'No'),
		2=>array('keyvalue' => 1, 'showvalue' => 'Yes')
	);
	
	function make_add_control($col_name,$colspec){
	    $ele_params['id'] = $col_name;
		$ele_params['name'] = $col_name;
		// adjusted this as we get away from using the select
		//$type = self::$ctl_mapping[$colspec['select']];
		$type = self::$ctl_mapping[$colspec['edit_as']];
		//print 'Type is ' . $type . "\n";
		if($type == 'CtlCheckbox'){
			$body = MakePhpCtl('CtlSelect',$ele_params,'',self::$yesno_entry);
							
		}else{
		
			$body = MakePhpCtl($type,$ele_params,'',@array_merge(self::$none_entry,$this->lookup_lists[$col_name]));
		}	
		return $body;
	}
		
	
	function __construct($fieldspec, $tablename, &$thefilters, $actionpage){
		$this->fieldspec = $fieldspec;
		$this->table_name = $table_name;
		$this->thefilters = &$thefilters;
		$this->actionpage = $actionpage;
	
	}


	function set_lookup_lists($ar_lists){
		$this->lookup_lists = $ar_lists;
	}
	
	
	function set_lookup_list($column,$list){
		$this->lookup_lists[$column] = $list;
		foreach($lookup AS $row=>$pair){
			$index[$lookup[$row]['keyvalue']] = $lookup[$row]['showvalue'];
		}
		$this->index_lookups[$column] = $index;
	}

	function add_form($fieldset,$filter,$filter_set){
		if(is_array($this->fieldspec)){
		foreach($this->fieldspec AS $col_name => $colspec){
			// only do something if it is [add]
			if(!empty($colspec['add'])){
				// if it is in the filter_set and [flt_add] is not 0 (defaults yes), add hidden
				if(isset($filter_set[$col_name]) AND ($colspec['flt_add'] !== 0)){
					$hiddenfields .= "<input type='hidden' name='$col_name' value='" . $filter_set[$col_name] . "' /> ";
				
				}else{
				
					if(!empty($colspec['headname'])) {
						$head = $colspec['headname'];
					}else{
						$head = $col_name;
					}
					
					$body = $this->make_add_control($col_name,$colspec);
					$elements[] = array('head'=>$head, 'body'=>$body);						
				}
			} // end [add]
		
		}
		}
		//$filterbox = "<form name='filters_box' action='$this->selfpage' method='$method'>\n";
        $filterbox .= $hiddenfields;
		$filterbox .= $this->make_element_set($elements, "class='add_head'","class='add_body'");
     	return $filterbox;
		
		
	}
	
	
// NOTE:  Add procedure adds whatever it is passed.  It is up to the add form to send the correct items.  If something is filterd, it needs to be sent as hidden.

	
	/**
	 *(Also in CF_Filter)
     * @make_element set will create a line of headed elements arranged in a table
     * from an input of an element_set array.  Each row has element_name and value
     * @param array elements An array with [body] and [head] parts where head
     * elements['head'] is the title and elements['body'] is what to put in the cell
     * @param string $headset Settings for heading elements (class name, etc)
     * @param string $bodyset Settings for body elemetns (class naem, etc)
	 *
	 */
    function make_element_set($elements, $headset, $bodyset){

        if(is_array($elements)){
        foreach ($elements AS $current){
            $headtext .= "<th>" . $current['head'] . "</th>\n";
            $bodytext .=  "<td>" . $current['body'] . "</td>\n";
        }
        }
        return "<tr $headset>$headtext </tr> <tr $bodyset>$bodytext</tr>";
    }

}
?>